import java.awt.Color;
import java.io.StringWriter;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;


public class T_ScrollAndSplitPane {
	
	public static void main(String[] args) {
		
		JFrame      frame;		
		JTextArea   textArea;
		JScrollPane scrollPane;
		JSplitPane  mainSplitPane;
		
		JSplitPane embeddedSplitPane;
		JPanel     redPanel;
		JPanel     blackPanel;
		
		Document tweetStream;
		
		String[] keywords = {"monkey", "pie"};
		
		tweetStream = TweetGrabber.getTweetsByScreenName("profhal");
		//tweetStream = TweetGrabber.getTweetsByKeywords(keywords);		
		
		
		// Create the frame and configure
		//
		frame = new JFrame("Scoll/Split Panes");
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frame.setSize(300, 200);
		frame.setLocation(400, 400);
		
		
		// Create the split pane. You can configure it to be split vertically or horizontally. You just
		// specify
		//
		//     JSplitPane.VERTICAL_SPLIT
		//
		// or
		//
		//     JSplitPane.HORIZONTAL_SPLIT
		//
		// in the constructor.
		// 
		mainSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
		
		frame.getContentPane().add(mainSplitPane);
		
		
		// Create the scroll pane. You can set the scroll bar policies for both the veritcal and horizontal 
		// scrollbars to always show the scroll bar, show the scroll bar only when it's needed or to never 
		// show the scroll bar. You can probably figure out how to change the below to try the different 
		// options.
		//
		scrollPane = new JScrollPane();
		
		scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
				
		// Lastly, create the text area to initially contain the XML retrieved from the Twitter call. Text
		// areas can be configured as editable and in-editable.
		//
		textArea = new JTextArea(getXMLAsString(tweetStream));
		
		textArea.setEditable(false);
		
		
		// Add the text area to the viewport of the scroll pane and place the scroll pane in one side or the 
		// other of the split pane.
		//
		scrollPane.setViewportView(textArea);
		
		mainSplitPane.add(scrollPane, JSplitPane.LEFT);
		
		
		// Create the embedded split pane with a red top component and a black bottom one. Also, enable 
		// "one touch" expansion.
		//
		// Place the embedded panel on the right side of the main split pane.
		//
		embeddedSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		
		redPanel = new JPanel();
		redPanel.setBackground(Color.RED);
		
		embeddedSplitPane.add(redPanel, JSplitPane.TOP);
		
		blackPanel = new JPanel();
		blackPanel.setBackground(Color.BLACK);
		
		embeddedSplitPane.add(blackPanel, JSplitPane.BOTTOM);		

		embeddedSplitPane.setOneTouchExpandable(true);


		mainSplitPane.add(embeddedSplitPane, JSplitPane.RIGHT);
		
		frame.setVisible(true);
		

		// These have to be set after the mainSplitPane and embeddedSplitPane are visible otherwise their width
		// is considered 0 so setting the location based on percentage (i.e., 0.5 means split the pane equally)
		// won't appear to work.
		//
		mainSplitPane.setDividerLocation(0.5);

		embeddedSplitPane.setDividerLocation(0.75);
				
	}
	
	
	// Dumps the specified document to the console. A little formatting is applied but it 
	// doesn't seem to be consistent. At least we can see the XML structure.
	//
	private static String getXMLAsString(Document d) {
		
		String result;
		
		Transformer transformer = null;
		
		DOMSource domSource=new DOMSource(d);
	    
	    StringWriter stringWriter = new StringWriter();
	    StreamResult streamResult = new StreamResult(stringWriter);
	    
	    // Create a Transformer to serialize the document
	    
	    try {
	
	    	transformer = TransformerFactory.newInstance().newTransformer();
	        transformer.setOutputProperty("indent","yes");
	    
	    } catch (TransformerConfigurationException t) {
	
	    	System.out.println("Document is not well formed.");
	    	System.out.println("   " + t.getMessage());        	
	    
	    }
	
	    // Transform the document to the result stream
	    try {
	
	    	transformer.transform(domSource, streamResult);        
	    
	    } catch (TransformerException t) {
			System.out.println("Error converting document to string.");
	    	System.out.println("   " + t.getMessage());        	
	    	
	    }

	    result = stringWriter.toString();
	    
	    return result;
		
	}
}